/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */

function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = next === undefined ? null : next;
}
var mergeTwoLists = function (list1, list2) {
  let pHead = new ListNode(-1);
  const p = pHead;
  while (list1 !== null && list2 !== null) {
    if (list1.val > list2.val) {
      pHead.next = list2;
      pHead = pHead.next;
      list2 = list2.next;
    } else {
      pHead.next = list1;
      pHead = pHead.next;
      list1 = list1.next;
    }
  }
  pHead.next = list1 === null ? list2 : list1;
  return p.next;
};

currentNode1 = {
  val: 0,
  next: {
    val: 1,
    next: {
      val: 1,
      next: {
        val: 3,
        next: null,
      },
    },
  },
};
currentNode2 = {
  val: 3,
  next: {
    val: 7,
    next: {
      val: 9,
      next: {
        val: 13,
        next: null,
      },
    },
  },
};
console.log(mergeTwoLists(currentNode1, currentNode2))
